/**
 * @file utils
 * @brief This file contains all the functions prototypes for the utils
 *        firmware library.
 * @author He Shaohua (hsh15529870639@foxmail.com)
 * @date 2024/11/13
 * @version 1.0
 * @copyright Copyright (c) 2024, SNUT-Chaoyue Team
 * ****************************************************************************************************
 * @par Change Log:
 * <table>
 * <tr><th>Date         <th>Version       <th>Author      <th>Description
 * <tr><td>2024/11/13      <td> 1.0          <td>He Shaohua  <td>Creator
 *
 * </table>
 * ==================================----How to use this
 * driver----=====================================
 * [..]
 * @note
 *   -#
 *
 * @warning
 *   -#
 *
 * *****************************************************************************************************
 * @attention
 *
 * This document is required and please understand any bugs that may occur in
 * the document. If you want to modify this file, after the code update is
 * completed, update the version number in time, indicate the update date, and
 * fill in the changes Make sure that the submitted code does not have many
 * bugs.
 *
 * All rights reserved.
 * *****************************************************************************************************
 */
/* Includes ------------------------------------------------------------------*/
#include "utils.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void can_init(uint32_t rx_id)
{
    FDCAN_FilterTypeDef CAN_FilterInitStructure;
    CAN_FilterInitStructure.IdType = FDCAN_STANDARD_ID;
    CAN_FilterInitStructure.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
    CAN_FilterInitStructure.FilterType = FDCAN_FILTER_MASK;
    CAN_FilterInitStructure.FilterIndex = 0;
    CAN_FilterInitStructure.FilterID1 = rx_id;
    CAN_FilterInitStructure.FilterID2 = 0X7ff;
    HAL_FDCAN_ConfigFilter(&hfdcan1, &CAN_FilterInitStructure);

  HAL_FDCAN_ConfigGlobalFilter(&hfdcan1,
    FDCAN_REJECT,
    FDCAN_REJECT,
    FDCAN_FILTER_REMOTE,
    FDCAN_FILTER_REMOTE);


   HAL_FDCAN_Start(&hfdcan1);
   HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE,0);
}

void can_send(const uint16_t tx_id, const uint8_t* tx_buffer)
{
    static FDCAN_TxHeaderTypeDef Tx_Header;
    Tx_Header.Identifier = tx_id;
    Tx_Header.IdType = FDCAN_STANDARD_ID;
    Tx_Header.TxFrameType = FDCAN_DATA_FRAME;
    Tx_Header.DataLength = FDCAN_DLC_BYTES_8;
    Tx_Header.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
    Tx_Header.BitRateSwitch = FDCAN_BRS_OFF;
    Tx_Header.FDFormat = FDCAN_CLASSIC_CAN;
    Tx_Header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
    Tx_Header.MessageMarker = 0x52;
    HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &Tx_Header, tx_buffer);
}


#define DATAADDR1 (0x08010000+8*0)

// 存储page1单字节32位数据page1大小2048
void Flash_Page1_Save(uint32_t *dataarr)
{
  uint32_t error = 0;
  uint32_t *pdata = dataarr;
  uint16_t length = sizeof(pdata) * 2;
  FLASH_EraseInitTypeDef flash_dat;
  HAL_FLASH_Unlock();
  flash_dat.TypeErase = FLASH_TYPEERASE_PAGES;
  flash_dat.Page = static_cast<uint32_t>((DATAADDR1 - 0x08000000) / 2048);
  flash_dat.NbPages = 1;
  flash_dat.Banks = 1;
  HAL_FLASHEx_Erase(&flash_dat, &error);
  static uint64_t data64;
  for (int i = 0; i < length; i++)
  {
    data64 = ((static_cast<uint64_t>(pdata[2 * i]) << 32) & 0xffffffff00000000) | ((static_cast<uint64_t>(pdata[2 * i + 1]) & 0x00000000ffffffff));
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, DATAADDR1 + i * 8, data64);
  }
  HAL_FLASH_Lock();
}
// 读取page1单字节32位数据page1大小2048
void Flash_Page1_Read(uint32_t *dataarr)
{
  uint64_t read_dat = 0;
  uint32_t add = DATAADDR1;
  uint32_t *pdata = dataarr;
  uint16_t length = sizeof(pdata) * 2;
  for (int i = 0; i < length; i++)
  {
    add = DATAADDR1 + i * 8;
    read_dat = *(__I uint64_t *)add;										// 读出flash中的数据
    pdata[2 * i] = read_dat >> 32;											// H
    pdata[2 * i + 1] = (read_dat & 0x00000000FFFFFFFF); // L
  }
}


uint32_t Float_HEX(float fdata)
{
  union u_tag u_tag1;
  u_tag1.f = fdata;
  return u_tag1.i;
}

float HEX_float(uint32_t data)
{
  union u_tag u_tag1;
  u_tag1.i = data;
  return u_tag1.f;
}
/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
/*********************************END OF FILE*********************************/
